/**
 * @Project TruckAssistant
 * @Filename Json.java
 * @Package cn.huayuansoft.utils
 * @Description TODO
 * @Version 1.0
 * @Author Administrator
 * @Creation 2014-12-23 娑撳宕�:56:33
 * @Mender Administrator
 * @Modification 2014-12-23 娑撳宕�:56:33
 * @Copyright Copyright 婕�2013-2014 HuaYuan Soft.All Rights Reserved.
 **/
package com.chookss.tools;

import android.content.Context;

import com.chookss.base.StaticClass;
import com.chookss.home.entity.HolidayDetailEntity;
import com.chookss.home.entity.LogEntity;
import com.chookss.home.entity.ReimbursementDetailEntity;
import com.chookss.home.entity.UploadEntity;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.johnrambo.ktea.common.klogutil.KLog;
import com.johnrambo.ktea.ktExtends.ShareUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class XJson {
    /**
     * 实体对象转成Map
     *
     * @param obj 实体对象
     * @return
     */
    public static Map<String, Object> object2Map(Object obj) {
        Map<String, Object> map = new HashMap<>();
        if (obj == null) {
            return map;
        }
        Class clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                map.put(field.getName(), field.get(obj));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * Map转成实体对象
     *
     * @param map   map实体对象包含属性
     * @param clazz 实体对象类型
     * @return
     */
    public static Object map2Object(Map<String, Object> map, Class<?> clazz) {
        if (map == null) {
            return null;
        }
        Object obj = null;
        try {
            obj = clazz.newInstance();

            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                int mod = field.getModifiers();
                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                    continue;
                }
                field.setAccessible(true);
                field.set(obj, map.get(field.getName()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static String listToString(List<Map<String, String>> list) {
        try {
            JSONArray jaBody = new JSONArray();
            Iterator<Map<String, String>> localIterator = list.iterator();
            while (localIterator.hasNext()) {
                jaBody.put(new JSONObject(localIterator.next()));
            }
            JSONObject jo = new JSONObject();

            jo.put("body", jaBody);
            return jo.toString();
        } catch (JSONException localJSONException) {
            return null;
        } catch (Exception localException) {
            return null;
        }
    }


    /**
     * @param infoMap
     * @return String
     * @Name mapToJsonObject
     * @Description 将map转化为jsonObject
     * @Author Administrator
     * @Date 2014-8-5 上午11:22:17
     **/
    public static String mapToJsonObject(Map<String, Object> infoMap) {
        JSONObject jObj = new JSONObject(infoMap);
        return jObj.toString();
    }

    /**
     * @param infoMap
     * @return String
     * @Name mapToJsonObject
     * @Description 将map转化为jsonObject
     * @Author Administrator
     * @Date 2014-8-5 上午11:22:17
     **/
    public static String mapToJsonObj(Map<String, String> infoMap) {
        JSONObject jObj = new JSONObject(infoMap);
        return jObj.toString();
    }

    public static JSONObject mapToJObj(Map<String, Object> infoMap) {
        JSONObject jObj = new JSONObject(infoMap);
        return jObj;
    }

    /**
     * @return String
     * @Name listToJsonArray
     * @Description 将list转化为JSONArray
     * @Author Administrator
     * @Date 2014-8-5 上午11:22:17
     **/
    public static JSONArray listToJsonArray(List<Map<String, Object>> infoList) {
        JSONArray jArray = new JSONArray();
        Iterator<Map<String, Object>> localIterator = infoList.iterator();
        while (localIterator.hasNext()) {
            jArray.put(new JSONObject(localIterator.next()));
        }
        return jArray;
    }

    public static String listToJsArray(List<Map<String, String>> infoList) {
        JSONArray jArray = new JSONArray();
        Iterator<Map<String, String>> localIterator = infoList.iterator();
        while (localIterator.hasNext()) {
            jArray.put(new JSONObject(localIterator.next()));
        }
        return jArray.toString();
    }

    public static JSONArray listToJsArrays(List<Map<String, String>> infoList) {
        JSONArray jArray = new JSONArray();
        Iterator<Map<String, String>> localIterator = infoList.iterator();
        while (localIterator.hasNext()) {
            jArray.put(new JSONObject(localIterator.next()));
        }
        return jArray;
    }

    public static JSONArray listStringToJsArray(List<String> list) {
        JSONArray jArray = new JSONArray();
        Iterator<String> localIterator = list.iterator();
        while (localIterator.hasNext()) {
            jArray.put(localIterator.next());
        }

        return jArray;

    }


    // Json字符串转化成Map对象

    public static Map<String, Object> parse(String jsonStr) {

        Map<String, Object> result = null;

        if (null != jsonStr) {
            try {

                JSONObject jsonObject = new JSONObject(jsonStr);
                result = parseJSONObject(jsonObject);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }

        return result;

    }

    public static Map<String, Object> parseJSONObject(JSONObject jsonObject) throws JSONException {

        Map<String, Object> valueObject = null;
        if (null != jsonObject) {
            valueObject = new HashMap<String, Object>();

            Iterator<String> keyIter = jsonObject.keys();
            while (keyIter.hasNext()) {
                String keyStr = keyIter.next();
                Object itemObject = jsonObject.opt(keyStr);
                if (null != itemObject) {
                    valueObject.put(keyStr, parseValue(itemObject));
                } // if (null != itemValueStr)

            } // while (keyIter.hasNext())
        } // if (null != valueStr)

        return valueObject;
    }


    public static Object parseValue(Object inputObject) throws JSONException {
        Object outputObject = null;

        if (null != inputObject) {

            if (inputObject instanceof JSONArray) {
                outputObject = parseJSONArray((JSONArray) inputObject);
            } else if (inputObject instanceof JSONObject) {
                outputObject = parseJSONObject((JSONObject) inputObject);
            } else if (inputObject instanceof String || inputObject instanceof Boolean || inputObject instanceof Integer) {
                outputObject = inputObject;
            }

        }

        return outputObject;


    }


    public static List<Object> parseJSONArray(JSONArray jsonArray) throws JSONException {

        List<Object> valueList = null;

        if (null != jsonArray) {
            valueList = new ArrayList<Object>();

            for (int i = 0; i < jsonArray.length(); i++) {
                Object itemObject = jsonArray.get(i);
                if (null != itemObject) {
                    valueList.add(parseValue(itemObject));
                }
            } // for (int i = 0; i < jsonArray.length(); i++)
        } // if (null != valueStr)

        return valueList;
    }


    /**
     * List转换String
     *
     * @param list :需要转换的List
     * @return String转换后的字符串
     */
    public static String ListToString(List<?> list) {
        StringBuffer sb = new StringBuffer();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) == null || list.get(i) == "") {
                    continue;
                }
                // 如果值是list类型则调用自己
                if (list.get(i) instanceof List) {
                    sb.append(ListToString((List<?>) list.get(i)));
                    sb.append(SEP1);
                } else if (list.get(i) instanceof Map) {
                    sb.append(MapToString((Map<?, ?>) list.get(i)));
                    sb.append(SEP1);
                } else {
                    sb.append(list.get(i));
                    sb.append(SEP1);
                }
            }
        }
        return "L" + sb.toString();
    }

    /**
     * Map转换String
     *
     * @param map :需要转换的Map
     * @return String转换后的字符串
     */
    public static String MapToString(Map<?, ?> map) {
        StringBuffer sb = new StringBuffer();
        // 遍历map
        for (Object obj : map.keySet()) {
            if (obj == null) {
                continue;
            }
            Object key = obj;
            Object value = map.get(key);
            if (value instanceof List<?>) {
                sb.append(key.toString() + SEP1 + ListToString((List<?>) value));
                sb.append(SEP2);
            } else if (value instanceof Map<?, ?>) {
                sb.append(key.toString() + SEP1 + MapToString((Map<?, ?>) value));
                sb.append(SEP2);
            } else {
                sb.append(key.toString() + SEP3 + value.toString());
                sb.append(SEP2);
            }
        }
        return "M" + sb.toString();
    }

    /**
     * String转换Map
     *
     * @param mapText :需要转换的字符串
     * @return Map<?, ?>
     */
    public static Map<String, Object> StringToMap(String mapText) {

        if (mapText == null || mapText.equals("")) {
            return null;
        }
        mapText = mapText.substring(1);

        Map<String, Object> map = new HashMap<String, Object>();
        String[] text = mapText.split("\\" + SEP2); // 转换为数组
        for (String str : text) {
            String[] keyText = str.split(SEP3); // 转换key与value的数组
            if (keyText.length < 1) {
                continue;
            }
            String key = keyText[0]; // key
            String value = keyText[1]; // value
            if (value.charAt(0) == 'M') {
                Map<?, ?> map1 = StringToMap(value);
                map.put(key, map1);
            } else if (value.charAt(0) == 'L') {
                List<?> list = StringToList(value);
                map.put(key, list);
            } else {
                map.put(key, value);
            }
        }
        return map;
    }

    /**
     * String转换List
     *
     * @param listText :需要转换的文本
     * @return List<?>
     */
    public static List<Object> StringToList(String listText) {
        if (listText == null || listText.equals("")) {
            return null;
        }
        listText = listText.substring(1);

        List<Object> list = new ArrayList<>();
        String[] text = listText.split("\\" + SEP1);
        String listStr = "";
        boolean flag = false;
        for (String str : text) {
            if (!str.equals("")) {
                if (str.charAt(0) == 'M') {
                    Map<?, ?> map = StringToMap(str);
                    list.add(map);
                } else if (str.charAt(0) == 'L' || flag) {
                    flag = true;
                    listStr += str + SEP1;
                } else {
                    list.add(str);
                }
            }
            if (str.equals("")) {
                flag = false;
                List<?> lists = StringToList(listStr);
                list.add(lists);
            }
        }
        return list;
    }

    /**
     * 定义分割常量
     * #用于list中每个元素间的分割
     * |用于map中每一个kv对间的分割
     * =用于map中key与value间的分割
     */
    private static final String SEP1 = ",";
    private static final String SEP2 = "|";
    private static final String SEP3 = "=";


    /**
     * 使用SharedPreferences保存List
     * 支持类型：List<String>，List<JavaBean>
     *
     * @param context   上下文
     * @param key       储存的key
     * @param dataList  存储数据
     * @param <Subject> 泛型
     */
    public static <Subject> void setDataList(Context context, String key, List<Subject> dataList) {
        if (null == dataList || dataList.size() < 0) {
            return;
        }

        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(dataList);
        new ShareUtils().putString(context, key, strJson);
    }

    public static <Subject> List<Subject> getDataList(Context context, String key, Class<Subject> cls) {
        List<Subject> list = new ArrayList<Subject>();
        String strJson = new ShareUtils().getString(context, key, "");
        if (Utils.isNull(strJson)) {
            return list;
        }

        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    public static <WorkNewEntity> List<WorkNewEntity> getWorkNewEntityList(Context context,String strJson,Class<WorkNewEntity> cls) {
        List<WorkNewEntity> list = new ArrayList<WorkNewEntity>();
        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    /**
     * 使用SharedPreferences保存List
     * 支持类型：List<String>，List<JavaBean>
     *
     * @param context         上下文
     * @param key             储存的key
     * @param dataList        存储数据
     * @param <AccountEntity> 泛型
     */
    public static <AccountEntity> void setAccountsList(Context context, String key, List<AccountEntity> dataList) {
        if (null == dataList || dataList.size() < 0) {
            return;
        }

        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(dataList);
        new ShareUtils().putString(context, "chookss_account", strJson);
    }

    public static <AccountEntity> List<AccountEntity> getAccountsList(Context context, String key, Class<AccountEntity> cls) {
        List<AccountEntity> list = new ArrayList<AccountEntity>();
        String strJson = new ShareUtils().getString(context, "chookss_account", "");
        if (Utils.isNull(strJson)) {
            return list;
        }

        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 使用SharedPreferences保存List
     * 支持类型：List<String>，List<JavaBean>
     *
     * @param context           上下文
     * @param dataList          存储数据
     * @param <WatermarkEntity> 泛型
     *  1题库 2小视频 3视频 4集合  5课程6资料库 7我的考试8学习计划 9个人能力  10段位 11我的代办12公告 13新闻14日志15通讯录16一键面试17全局搜索18个人资料19通知20收藏
     */
    public static <WatermarkEntity> void setWatermarkList(Context context, List<WatermarkEntity> dataList) {
        if (null == dataList || dataList.size() < 1) {
            return;
        }

        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(dataList);
        new ShareUtils().putString(context, "watermark", strJson);
    }

    public static <WatermarkEntity> List<WatermarkEntity> getWatermarkList(Context context, Class<WatermarkEntity> cls) {
        List<WatermarkEntity> list = new ArrayList<WatermarkEntity>();
        String strJson = new ShareUtils().getString(context, "watermark", "");
        if (Utils.isNull(strJson)) {
            return list;
        }

        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static <LogEntity> List<LogEntity> getLogCache(Context context, Class<LogEntity> cls) {
        List<LogEntity> list = new ArrayList<LogEntity>();
        String employeecode = new ShareUtils().getString(context, StaticClass.EMPLOYEECODE, "");
        String strJson = new ShareUtils().getString(context, employeecode + "_logentity", "");
        KLog.i(list.size() + "--===");
        if (Utils.isNull(strJson)) {
            return list;
        }
        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        KLog.i(list.size() + "===");
        return list;
    }
    public static <HolidayDetailEntity> List<HolidayDetailEntity> getHolidayDetailEntityCache(Context context, Class<HolidayDetailEntity> cls) {
        List<HolidayDetailEntity> list = new ArrayList<HolidayDetailEntity>();
        String employeecode = new ShareUtils().getString(context, StaticClass.EMPLOYEECODE, "");
        String strJson = new ShareUtils().getString(context, employeecode + "_HolidayDetailEntity", "");
        if (Utils.isNull(strJson)) {
            return list;
        }
        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    public static <ReimbursementDetailEntity> List<ReimbursementDetailEntity> getReimbursementDetailEntityCache(Context context,String init, Class<ReimbursementDetailEntity> cls) {
        List<ReimbursementDetailEntity> list = new ArrayList<ReimbursementDetailEntity>();
        String employeecode = new ShareUtils().getString(context, StaticClass.EMPLOYEECODE, "");
        String strJson = new ShareUtils().getString(context, employeecode +init+ "_ReimbursementDetailEntity", "");
        if (Utils.isNull(strJson)) {
            return list;
        }
        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    /**
     * 使用SharedPreferences保存List
     * 支持类型：List<String>，List<JavaBean>
     *
     * @param context 上下文
     */
    public static void setLogCache(Context context, int type, LogEntity logEntity) {
//        0 上传中  1成功 2失败3 重新上传
        if (Utils.isNull(logEntity.getCache())) {
            return;
        }
        List<LogEntity> list = new ArrayList<LogEntity>();
        String strJson = new ShareUtils().getString(context, logEntity.getEmployeeCode() + "_logentity", "");
        try {
            if (!Utils.isNull(strJson)) {
                Gson gson = new Gson();
                JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
                for (JsonElement jsonElement : arry) {
                    list.add(gson.fromJson(jsonElement, LogEntity.class));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (type == 0) {
            list.add(logEntity);
        } else if (type == 1) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCache().equals(logEntity.getCache())) {
                    list.remove(i);
                    if (i < list.size()) {
                        i--;
                    }
                }
            }
        } else if (type == 2) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCache().equals(logEntity.getCache())) {
                    list.get(i).setCacheStatus(2);
                }
            }
        } else if (type == 3) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCache().equals(logEntity.getCache())) {
                    list.get(i).setCacheStatus(0);
                }
            }
        }
        if (list.size() == 0) {
            new ShareUtils().putString(context, logEntity.getEmployeeCode() + "_logentity", "");
        } else {
            Gson gson = new Gson();
            //转换成json数据，再保存
            String newStrJson = gson.toJson(list);
            new ShareUtils().putString(context, logEntity.getEmployeeCode() + "_logentity", newStrJson);
        }
    }
    /**
     * 使用SharedPreferences保存List
     * 支持类型：List<String>，List<JavaBean>
     *
     * @param context 上下文
     */
    public static void setHolidayDetailEntityCache(Context context, int type, HolidayDetailEntity holidayDetailEntity) {
//        0 上传中  1成功 2失败3 重新上传
        if (Utils.isNull(holidayDetailEntity.getCache())) {
            return;
        }
        List<HolidayDetailEntity> list = new ArrayList<HolidayDetailEntity>();
        String strJson = new ShareUtils().getString(context, holidayDetailEntity.getEmployeeCode() + "_HolidayDetailEntity", "");
        try {
            if (!Utils.isNull(strJson)) {
                Gson gson = new Gson();
                JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
                for (JsonElement jsonElement : arry) {
                    list.add(gson.fromJson(jsonElement, HolidayDetailEntity.class));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (type == 0) {
            list.add(holidayDetailEntity);
        } else if (type == 1) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCache().equals(holidayDetailEntity.getCache())) {
                    list.remove(i);
                    if (i < list.size()) {
                        i--;
                    }
                }
            }
        } else if (type == 2) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCache().equals(holidayDetailEntity.getCache())) {
                    list.get(i).setCacheStatus(2);
                }
            }
        } else if (type == 3) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCache().equals(holidayDetailEntity.getCache())) {
                    list.get(i).setCacheStatus(0);
                }
            }
        }
        if (list.size() == 0) {
            new ShareUtils().putString(context, holidayDetailEntity.getEmployeeCode() + "_HolidayDetailEntity", "");
        } else {
            Gson gson = new Gson();
            //转换成json数据，再保存
            String newStrJson = gson.toJson(list);
            new ShareUtils().putString(context, holidayDetailEntity.getEmployeeCode() + "_HolidayDetailEntity", newStrJson);
        }
    }
    /**
     * 使用SharedPreferences保存List
     * 支持类型：List<String>，List<JavaBean>
     * init 物料和借款要有，默认是报销的
     * @param context 上下文
     */
    public static void setReimbursementDetailEntityCache(Context context, int type,String init, ReimbursementDetailEntity reimbursementDetailEntity) {
//        0 上传中  1成功 2失败3 重新上传
        if (Utils.isNull(reimbursementDetailEntity.getCache())) {
            return;
        }
        List<ReimbursementDetailEntity> list = new ArrayList<ReimbursementDetailEntity>();
        String strJson = new ShareUtils().getString(context, reimbursementDetailEntity.getEmployeeCode() +init+ "_ReimbursementDetailEntity", "");
        try {
            if (!Utils.isNull(strJson)) {
                Gson gson = new Gson();
                JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
                for (JsonElement jsonElement : arry) {
                    list.add(gson.fromJson(jsonElement, ReimbursementDetailEntity.class));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (type == 0) {
            list.add(reimbursementDetailEntity);
        } else if (type == 1) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCache().equals(reimbursementDetailEntity.getCache())) {
                    list.remove(i);
                    if (i < list.size()) {
                        i--;
                    }
                }
            }
        } else if (type == 2) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCache().equals(reimbursementDetailEntity.getCache())) {
                    list.get(i).setCacheStatus(2);
                }
            }
        } else if (type == 3) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getCache().equals(reimbursementDetailEntity.getCache())) {
                    list.get(i).setCacheStatus(0);
                }
            }
        }
        if (list.size() == 0) {
            new ShareUtils().putString(context, reimbursementDetailEntity.getEmployeeCode() +init + "_ReimbursementDetailEntity", "");
        } else {
            Gson gson = new Gson();
            //转换成json数据，再保存
            String newStrJson = gson.toJson(list);
            new ShareUtils().putString(context, reimbursementDetailEntity.getEmployeeCode()  +init+ "_ReimbursementDetailEntity", newStrJson);
        }
    }
    /**
     * 使用SharedPreferences保存List
     * 支持类型：List<String>，List<JavaBean>
     *
     * @param context 上下文
     */
    public static void setLogFailure(Context context, String employeecode) {
//        上传异常闪退的时候，重启app 默认缓存都是失败数据
        List<LogEntity> list = new ArrayList<LogEntity>();
        String strJson = new ShareUtils().getString(context, employeecode + "_logentity", "");
        try {
            if (!Utils.isNull(strJson)) {
                Gson gson = new Gson();
                JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
                for (JsonElement jsonElement : arry) {
                    list.add(gson.fromJson(jsonElement, LogEntity.class));
                }
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setCacheStatus(2);
                }
                Gson gson2 = new Gson();
                //转换成json数据，再保存
                String newStrJson = gson2.toJson(list);
                new ShareUtils().putString(context, employeecode + "_logentity", newStrJson);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        List<HolidayDetailEntity> holidayDetailEntityList = new ArrayList<HolidayDetailEntity>();
        String holidayDetailEntityJson = new ShareUtils().getString(context, employeecode + "_HolidayDetailEntity", "");
        try {
            if (!Utils.isNull(holidayDetailEntityJson)) {
                Gson gson = new Gson();
                JsonArray arry = new JsonParser().parse(holidayDetailEntityJson).getAsJsonArray();
                for (JsonElement jsonElement : arry) {
                    holidayDetailEntityList.add(gson.fromJson(jsonElement, HolidayDetailEntity.class));
                }
                for (int i = 0; i < holidayDetailEntityList.size(); i++) {
                    holidayDetailEntityList.get(i).setCacheStatus(2);
                }
                Gson gson2 = new Gson();
                //转换成json数据，再保存
                String newStrJson = gson2.toJson(holidayDetailEntityList);
                new ShareUtils().putString(context, employeecode + "_HolidayDetailEntity", newStrJson);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        List<ReimbursementDetailEntity> reimbursementDetailEntityList = new ArrayList<ReimbursementDetailEntity>();
        String reimbursementDetailEntityJson = new ShareUtils().getString(context, employeecode + "_ReimbursementDetailEntity", "");
        String shopDetailEntityJson = new ShareUtils().getString(context, employeecode + "shop_ReimbursementDetailEntity", "");
        try {
            if (!Utils.isNull(reimbursementDetailEntityJson)) {
                Gson gson = new Gson();
                JsonArray arry = new JsonParser().parse(reimbursementDetailEntityJson).getAsJsonArray();
                for (JsonElement jsonElement : arry) {
                    reimbursementDetailEntityList.add(gson.fromJson(jsonElement, ReimbursementDetailEntity.class));
                }
                for (int i = 0; i < reimbursementDetailEntityList.size(); i++) {
                    reimbursementDetailEntityList.get(i).setCacheStatus(2);
                }
                Gson gson2 = new Gson();
                //转换成json数据，再保存
                String newStrJson = gson2.toJson(reimbursementDetailEntityList);
                new ShareUtils().putString(context, employeecode + "_ReimbursementDetailEntity", newStrJson);
            }
            if (!Utils.isNull(shopDetailEntityJson)) {
                Gson gson = new Gson();
                JsonArray arry = new JsonParser().parse(shopDetailEntityJson).getAsJsonArray();
                for (JsonElement jsonElement : arry) {
                    reimbursementDetailEntityList.add(gson.fromJson(jsonElement, ReimbursementDetailEntity.class));
                }
                for (int i = 0; i < reimbursementDetailEntityList.size(); i++) {
                    reimbursementDetailEntityList.get(i).setCacheStatus(2);
                }
                Gson gson2 = new Gson();
                //转换成json数据，再保存
                String newStrJson = gson2.toJson(reimbursementDetailEntityList);
                new ShareUtils().putString(context, employeecode + "shop_ReimbursementDetailEntity", newStrJson);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用SharedPreferences保存List
     * 支持类型：List<String>，List<JavaBean>
     *
     * @param context  上下文
     * @param dataList 存储数据
     */
    public static <KnowledgeEntity> void setKnowledgeEntity(Context context, List<KnowledgeEntity> dataList) {
        if (null == dataList || dataList.size() < 1) {
            return;
        }
        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(dataList);
        new ShareUtils().putString(context, new ShareUtils().getString(context, StaticClass.EMPLOYEECODE, "") + "KnowledgeEntity", strJson);
    }

    public static <KnowledgeEntity> List<KnowledgeEntity> getKnowledgeEntity(Context context, Class<KnowledgeEntity> cls) {
        List<KnowledgeEntity> list = new ArrayList<KnowledgeEntity>();
        String strJson = new ShareUtils().getString(context, new ShareUtils().getString(context, StaticClass.EMPLOYEECODE, "") + "KnowledgeEntity", "");
        if (Utils.isNull(strJson)) {
            return list;
        }

        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 使用SharedPreferences保存List
     * 支持类型：List<String>，List<JavaBean>
     *
     * @param context  上下文
     */
    public static void setUploadEntity(Context context, Class<UploadEntity> cls) {
        List<UploadEntity> list = new ArrayList<UploadEntity>();
        String strJson = new ShareUtils().getString(context, new ShareUtils().getString(context, StaticClass.EMPLOYEECODE, "") + "UploadEntity", "");
        if (!Utils.isNull(strJson)) {
            try {
                Gson gson = new Gson();
                JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
                for (JsonElement jsonElement : arry) {
                    list.add(gson.fromJson(jsonElement, cls));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i < list.size(); i++) {
            //上传失败的全部给与状态2
            list.get(i).setUploadStatus(2);
        }
        Gson gson2 = new Gson();
        //转换成json数据，再保存
        String strJson2 = gson2.toJson(list);
        new ShareUtils().putString(context, new ShareUtils().getString(context, StaticClass.EMPLOYEECODE, "") + "UploadEntity", strJson2);
    }

    public static void addUploadEntity(Context context, List<UploadEntity> lists, Class<UploadEntity> cls) {
        if (null == lists ) {
            return;
        }
        if (lists.size()==0 ) {
            return;
        }
        List<UploadEntity> list = new ArrayList<UploadEntity>();
        String strJson = new ShareUtils().getString(context, lists.get(0).getProvideEmployeeCode() + "UploadEntity", "");
        if (!Utils.isNull(strJson)) {
            try {
                Gson gson = new Gson();
                JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
                for (JsonElement jsonElement : arry) {
                    list.add(gson.fromJson(jsonElement, cls));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i <lists.size() ; i++) {
            for (int j = 0; j < list.size(); j++) {
                if(list.get(j).getUploadTag().equals(lists.get(i).getUploadTag())){
                    list.remove(j);
                    if(j<list.size()){
                        j--;
                    }
                }
            }
        }
        list.addAll(lists);
        Gson gson2 = new Gson();
        //转换成json数据，再保存
        String strJson2 = gson2.toJson(list);
        new ShareUtils().putString(context, lists.get(0).getProvideEmployeeCode() + "UploadEntity", strJson2);
        EventBus.getDefault().post(new MyEvent("refreshUploadRecord"));

    }
    public static  void deleteUploadEntity(Context context, UploadEntity uploadEntity, Class<UploadEntity> cls) {
        if (null ==uploadEntity) {
            return;
        }
        List<UploadEntity> list = new ArrayList<UploadEntity>();
        String strJson = new ShareUtils().getString(context, uploadEntity.getProvideEmployeeCode() + "UploadEntity", "");
        if (!Utils.isNull(strJson)) {
            try {
                Gson gson = new Gson();
                JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
                for (JsonElement jsonElement : arry) {
                    list.add(gson.fromJson(jsonElement, cls));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUploadTag().equals(uploadEntity.getUploadTag())){
                list.remove(i);
                if(i<list.size()){
                    i--;
                }
            }
        }
        Gson gson2 = new Gson();
        //转换成json数据，再保存
        String strJson2 = gson2.toJson(list);
        new ShareUtils().putString(context, uploadEntity.getProvideEmployeeCode() + "UploadEntity", strJson2);
    }
    public static <UploadEntity> List<UploadEntity> getUploadEntity(Context context, Class<UploadEntity> cls) {
        List<UploadEntity> list = new ArrayList<UploadEntity>();
        String strJson = new ShareUtils().getString(context, new ShareUtils().getString(context, StaticClass.EMPLOYEECODE, "") + "UploadEntity", "");
        if (Utils.isNull(strJson)) {
            return list;
        }

        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
